package libs.yzx.zaza.acts;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.security.interfaces.RSAPublicKey;

import libs.yzx.zaza.zaza2.AESUtil;
import libs.yzx.zaza.zaza2.RSAUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/9/8
 */
public class RSAActivity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try{
            /* AES的key（应该是随机生成 , 需要16位） */
            String aes_key = "123456789abcdddd";
            /* 获取RSA的公钥 */
            RSAPublicKey pk = getPublicKeyFromAsset();
            /* 明文 */
            String data = "hello , fuck you";

            /* 实际数据的密文 */
            String dataMI = AESUtil.encrypt(data , aes_key);
            /* AES key的密文 */
            String keyMI = RSAUtils.encryptByPublicKey(aes_key,pk);

            /* 发送密文 */
            FormBody body = new FormBody.Builder().add("data", dataMI).add("key",keyMI).build();

            Request request = new Request.Builder().url("http://192.168.10.159:8080/cache").post(body).build();
            new OkHttpClient().newCall(request).enqueue(new Callback() {
                public void onResponse(Call call, final Response response) throws IOException {
                    final String str = response.body().string();
                    runOnUiThread(new Runnable() {
                        public void run() {
                            try{
                                Log.e("--.." , str);
                                Toast.makeText(RSAActivity.this, str, Toast.LENGTH_SHORT).show();
                            }
                            catch(Exception e){
                                e.printStackTrace();
                                Toast.makeText(RSAActivity.this, "onResponse -> ERROR", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                public void onFailure(Call call, IOException e) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(RSAActivity.this, "over but err", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });

        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(RSAActivity.this, "err : " + e.toString(), Toast.LENGTH_SHORT).show();
        }

    }


    /* 将assets的key文件放到本地，再用ObjectInputStream读取 */
    private RSAPublicKey getPublicKeyFromAsset(){
        try{
            InputStream in = getAssets().open("publicKey.key");
            File targetFile = new File(getFilesDir(), "pk");
            FileOutputStream out = new FileOutputStream(targetFile);
            byte[] bytes = new byte[in.available()];
            in.read(bytes);
            in.close();
            out.write(bytes);
            out.flush();
            out.close();
            ObjectInputStream oin = new ObjectInputStream(new FileInputStream(targetFile));
            Object key = oin.readObject();
            oin.close();
            return (RSAPublicKey) key;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
